//
//  RegisterViewController.m
//  Before
//
//  Created by lanou3g on 15/11/30.
//  Copyright © 2015年 sss. All rights reserved.
//

#import "RegisterViewController.h"
#import "AVOSCloud.h"
#import "MBProgressHUD.h"
#import <SMS_SDK/SMSSDK.h>
#import "MessageViewController.h"
#import "LogRegisterHelper.h"
#import "currentState.h"
#import "ScreenViewController.h"
@interface RegisterViewController ()
@property (nonatomic,strong)UITextField * nameTF;
@property (nonatomic,strong)UILabel * nameLabel;
@property (nonatomic,strong)UITextField * passwordTF;
@property (nonatomic,strong)UILabel * passwordLabel;
@property (nonatomic,strong)UITextField * repasswordTF;
@property (nonatomic,strong)UILabel * repasswordLabel;
@property (nonatomic,strong)UITextField * tellTF;
@property (nonatomic,strong)UILabel * tellLabel;
@property (nonatomic,strong)UITextField * verifyTF;
@property (nonatomic,strong)UIButton * verifyButton;
@property (nonatomic,strong)UITextField * emailTF;
@property (nonatomic,strong)UILabel * emailLabel;


@property (nonatomic,strong)UIButton * Registerbutton;
@property (nonatomic,strong)UIButton * backButton;
@property (nonatomic,strong)UILabel * imageLabel;
@property (nonatomic,strong)UIImageView * imageV;
@property (nonatomic,strong)AVUser * user;
@property (nonatomic,strong)MBProgressHUD * hud;
@end

@implementation RegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self makeData];
    self.user = [AVUser user];
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"register"]];
    
    // 注册按钮
    self.Registerbutton = [UIButton buttonWithType:(UIButtonTypeSystem)];
    [self.Registerbutton setTitle:@"注册" forState:(UIControlStateNormal)];
    self.Registerbutton.frame = CGRectMake(80, 590, 50, 50);
    [self.view addSubview:self.Registerbutton];
    [self.Registerbutton addTarget:self action:@selector(buttonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    // 返回 按钮
    self.backButton = [UIButton buttonWithType:(UIButtonTypeSystem)];
    [self.backButton setTitle:@"返回" forState:(UIControlStateNormal)];
    self.backButton.frame = CGRectMake(250, 590, 50, 50);
    [self.view addSubview:self.backButton];
    [self.backButton addTarget:self action:@selector(backbuttonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
   }
- (void)tapAction:(id)sender
{
    UIImagePickerController * picker = [[UIImagePickerController alloc] init];
    picker.allowsEditing = YES;
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    picker.delegate = self;
    [self presentViewController:picker animated:YES completion:^{
        
    }];
    
    
}
// 添加 头像
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
    
    UIImage * i = info[UIImagePickerControllerEditedImage];
    self.imageV.image = i;
    [self dismissViewControllerAnimated:YES completion:^{
    }];
    NSData * imageData = UIImagePNGRepresentation(self.imageV.image);
    AVFile * imageFile = [AVFile fileWithName:@"image.png" data:imageData];
    [imageFile saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        
        [self.user setObject:imageFile forKey:@"image"];
        
    }];
}
// 返回
- (void)backbuttonAction:(id)sender
{
    
    ScreenViewController * scree  = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"screenVC"];
    [self showViewController:scree sender:nil];
    
}
// 注册
- (void)buttonAction:(id)sender
{
    
    
    if (self.nameTF.text.length == 0) {
        [self showLetter:@"用户名不能为空"];
    }else if (self.passwordTF.text.length == 0 || self.repasswordTF.text.length == 0) {
        [self showLetter:@"密码不能为空"];
    }else if (![self.passwordTF.text isEqualToString:self.repasswordTF.text]) {
        [self showLetter:@"两次输入的密码不同，请重新输入"];
    }else{
        
        self.user.username = self.nameTF.text;
        self.user.password = self.passwordTF.text;
        self.user.email = self.emailTF.text;
        self.user.mobilePhoneNumber = self.tellTF.text;
        
        [SMSSDK commitVerificationCode:self.verifyTF.text phoneNumber:self.tellTF.text zone:@"86" result:^(NSError *error) {
            if (!error) {
                [self.user signUpInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
                    if (succeeded) {
                    [currentState defaultState].isLoading = YES;
                    [self showLetter:@"注册成功"];
                    [[LogRegisterHelper Share] Enter];
                        
                    }else if(error.code == 125){
                        [self showLetter:@"邮箱格式不正确"];
                    }else if (error.code == 127){
                        [self showLetter:@"电话格式不正确"];
                    }else if (error.code == 202){
                        [self showLetter:@"用户名已存在"];
                    }else if (error.code == 214){
                        [self showLetter:@"此电话号码已注册过"];
                    }else if (error.code == 203){
                        [self showLetter:@"此邮箱已经注册过，请直接登录"];
                    }
                }];
                
            }else
            {
                NSLog(@"%@",error);
                dispatch_async(dispatch_get_main_queue(), ^{
                    self.verifyTF.text = nil;
                    [self showLetter:@"请重新验证"];
                });
                
            }
        }];
    }
    
}
// MBP 显示
- (void)showLetter:(NSString *)title
{
    // 获取 window
    self.hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    // 仅仅显示文字
    self.hud.mode = MBProgressHUDModeText;
    // 进度条出现时 给整个界面加背景
    self.hud.dimBackground = NO;
    self.hud.labelText = title;
    [self.hud hide:YES afterDelay:2];
    
    
}

// 页面 制作
- (void)makeData
{
    // 头像
    self.imageLabel = [[UILabel alloc] initWithFrame:CGRectMake(140, 80, 100, 100)];
    [self.view addSubview:self.imageLabel];
    self.imageLabel.numberOfLines = 0;
    self.imageLabel.textAlignment = NSTextAlignmentCenter;
    self.imageLabel.text = @"点击上传头像";
    self.imageV = [[UIImageView alloc] initWithFrame:CGRectMake(140, 80, 100, 100)];
    self.imageV.layer.borderWidth = 1;
    self.imageV.layer.borderColor = [[UIColor  whiteColor] CGColor];
    self.imageV.layer.cornerRadius = 10;
    self.imageV.layer.masksToBounds = YES;
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    self.imageV.userInteractionEnabled = YES;
    [self.imageV addGestureRecognizer:tap];

    [self.view addSubview:self.imageV];
    // 昵称
    self.nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(80, 210, 100, 50)];
        [self.view addSubview:self.nameLabel];
    self.nameLabel.text = @"昵称:";
    self.nameTF = [[UITextField alloc] initWithFrame:CGRectMake(160, 220, 170, 30)];
    self.nameTF.placeholder = @"请输入昵称";
    self.nameTF.layer.cornerRadius = 10;
    self.nameTF.textAlignment = NSTextAlignmentCenter;
    self.nameTF.layer.borderWidth = 0.1;
    [self.view addSubview:self.nameTF];
    // 密码
    self.passwordLabel = [[UILabel alloc] initWithFrame:CGRectMake(80, 270, 100, 50)];
    self.passwordLabel.text = @"密码:";
    [self.view addSubview:self.passwordLabel];
    self.passwordTF = [[UITextField alloc] initWithFrame:CGRectMake(160, 280, 170, 30)];
    self.passwordTF.placeholder = @"请输入密码";
    self.passwordTF.layer.cornerRadius = 10;
    self.passwordTF.textAlignment = NSTextAlignmentCenter;
    self.passwordTF.layer.borderWidth = 0.1;
    [self.view addSubview:self.passwordTF];
    // 重复密码
    self.repasswordLabel = [[UILabel alloc] initWithFrame:CGRectMake(80, 330, 100, 50)];
    self.repasswordLabel.text = @"重复密码:";
    [self.view addSubview:self.repasswordLabel];
    self.repasswordTF = [[UITextField alloc] initWithFrame:CGRectMake(160, 340, 170, 30)];
    self.repasswordTF.placeholder = @"请重新输入密码";
    self.repasswordTF.layer.cornerRadius = 10;
    self.repasswordTF.textAlignment = NSTextAlignmentCenter;
    self.repasswordTF.layer.borderWidth = 0.1;
    [self.view addSubview:self.repasswordTF];
    // 电话
    self.tellLabel = [[UILabel alloc] initWithFrame:CGRectMake(80, 390, 100, 50)];
    self.tellLabel.text = @"电话:";
    [self.view addSubview:self.tellLabel];
    self.tellTF = [[UITextField alloc] initWithFrame:CGRectMake(160, 400, 170, 30)];
    self.tellTF.placeholder = @"请输入电话";
    self.tellTF.layer.cornerRadius = 10;
    self.tellTF.textAlignment = NSTextAlignmentCenter;
    self.tellTF.layer.borderWidth = 0.1;
    [self.view addSubview:self.tellTF];
    // 邮箱
    self.emailLabel = [[UILabel alloc] initWithFrame:CGRectMake(80, 450, 100, 50)];
    [self.view addSubview:self.emailLabel];
    self.emailLabel.text = @"邮箱:";
    self.emailTF = [[UITextField alloc] initWithFrame:CGRectMake(160, 460, 170, 30)];
    self.emailTF.placeholder = @"请输入邮箱";
    self.emailTF.layer.cornerRadius = 10;
    self.emailTF.textAlignment = NSTextAlignmentCenter;
    self.emailTF.layer.borderWidth = 0.1;
    [self.view addSubview:self.emailTF];
    
    // 验证码
    self.verifyButton = [UIButton buttonWithType:(UIButtonTypeSystem)];
    self.verifyButton.frame = CGRectMake(70, 520, 70, 50);
    self.verifyButton.titleLabel.numberOfLines = 0;
    [self.view addSubview:self.verifyButton];
    [self.verifyButton setTitle:@"点击获取验证码:" forState:(UIControlStateNormal)];
    [self.verifyButton addTarget:self action:@selector(getAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    self.verifyTF = [[UITextField alloc] initWithFrame:CGRectMake(160, 530, 170, 30)];
    self.verifyTF.placeholder = @"请输入验证码";
    self.verifyTF.layer.cornerRadius = 10;
    self.verifyTF.textAlignment = NSTextAlignmentCenter;
    self.verifyTF.layer.borderWidth = 0.1;
    [self.view addSubview:self.verifyTF];
    
}
// 回收键盘
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.nameTF resignFirstResponder];
    [self.passwordTF resignFirstResponder];
    [self.repasswordTF resignFirstResponder];
    [self.tellTF resignFirstResponder];
    [self.emailTF resignFirstResponder];
    [self.verifyTF resignFirstResponder];
}
// 验证码 响应事件
- (void)getAction:(id)sender
{
    if (self.tellTF.text == 0) {
        [self showLetter:@"请输入电话"];
    }
    [SMSSDK getVerificationCodeByMethod:SMSGetCodeMethodSMS phoneNumber:self.tellTF.text zone:@"86" customIdentifier:nil result:^(NSError *error) {
        if (!error) {
            [self showLetter:@"验证码发送成功"];
        }else
        {
            [self showLetter:@"电话不正确"];
        }
        
    }];
    
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
